    package com.chezhujie.www.controller.api;

    import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.chezhujie.www.domain.ImSMSstorage;
import com.chezhujie.www.service.ImSMSstorageService;
import com.xy.core.utils.CheckSumBuilder;
import com.xy.core.utils.ConvertObj;
import com.xy.core.utils.LocalDateTime;
import com.xy.core.utils.UUIDUtils;
import com.xy.user.base.controller.BaseController;

    @Controller
    @RequestMapping(value = {"/route"})
    public class RouteController extends BaseController{
        public static final Logger logger = LoggerFactory
                .getLogger(RouteController.class);
        
        @Autowired
        private ImSMSstorageService imSMSstorageService;
    
        
        // 需要改成自身应用的appSecret
        private final String appSecret = "266e5e39df5c";
        @SuppressWarnings("unchecked")
		@RequestMapping(value = {"/mockClient.action"}, method = {RequestMethod.POST})
        public void mockClient(HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            System.out.println(LocalDateTime.now()+" 进入通讯回执接口");
            JSONObject result = new JSONObject();
            try {
                // 获取请求体
                byte[] body = readBody(request);
                if (body == null) {
                    logger.warn("request wrong, empty body!");
                    result.put("code", 414);
                    writeJson(response,JSONObject.toJSONString(result));
                    return ;
                }
                // 获取部分request header，并打印
                String ContentType = request.getContentType();
                String AppKey = request.getHeader("AppKey");
                String CurTime = request.getHeader("CurTime");
                String MD5 = request.getHeader("MD5");
                String CheckSum = request.getHeader("CheckSum");
                logger.info("request headers: ContentType = {}, AppKey = {}, CurTime = {}, " +
                        "MD5 = {}, CheckSum = {}", ContentType, AppKey, CurTime, MD5, CheckSum);
                // 将请求体转成String格式，并打印
                String requestBody = new String(body, "utf-8");
                logger.info("request body = {}", requestBody);
                // 获取计算过的md5及checkSum
                String verifyMD5 = CheckSumBuilder.getMD5(requestBody);
                String verifyChecksum = CheckSumBuilder.getCheckSum(appSecret, verifyMD5, CurTime);
                logger.debug("verifyMD5 = {}, verifyChecksum = {}", verifyMD5, verifyChecksum);
                // TODO: 比较md5、checkSum是否一致，以及后续业务处理
                
                Map<String,Object> map = JSONObject.parseObject(requestBody, Map.class);
                if("11".equals((String)map.get("eventType"))){
                	for(Map<String,Object> object : ((List<Map<String, Object>>)map.get("objects"))){
                		ImSMSstorage imSMSstorage = new ImSMSstorage();
                		ImSMSstorage imSMSstorage2 = ConvertObj.map2Bean(imSMSstorage, object);
                		imSMSstorage2.setSmsstorageId(UUIDUtils.getUUID());
                		imSMSstorage2.setCreatetime(new Date());
                		imSMSstorageService.insert(imSMSstorage2);
                	}
                }
                
                result.put("code", 200);
                System.out.println(LocalDateTime.now()+" 发送通讯消息成功");
                writeJson(response,JSONObject.toJSONString(result));
                return ;
            } catch (Exception ex) {
                logger.error(ex.getMessage(), ex);
                result.put("code", 414);
                System.out.println(LocalDateTime.now()+" 发送通讯消息失败");
                writeJson(response,JSONObject.toJSONString(result));
                return ;
            }
        }
        private byte[] readBody(HttpServletRequest request) throws IOException {
            if (request.getContentLength() > 0) {
                byte[] body = new byte[request.getContentLength()];
                IOUtils.readFully(request.getInputStream(), body);
                return body;
            } else
                return null;
        }

    }

